<?php
/**
 * 订单表
 * @author RenJianHong
 * @date 2021-8-3 9:28
 */
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateOrderTable extends Migration
{
    public $tableName = "order";

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if(!Schema::hasTable($this->tableName)) $this->create();
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->tableName);
    }

    /**
     * 执行创建表
     */
    private function create()
    {
        Schema::create($this->tableName, function (Blueprint $table) {
            $table->engine = 'InnoDB';      // 设置存储引擎
            $table->charset = 'utf8';       // 设置字符集
            $table->collation  = 'utf8_general_ci';       // 设置排序规则
            $table->index('id');//普通索引
            $table->index('user_id');//普通索引

            $table->id();
            $table->integer("user_id")->nullable(false)->default(0)->comment("用户ID");
            $table->integer("mch_id")->nullable(false)->default(0)->comment("商户ID");
            $table->string("order_no")->nullable(false)->default(null)->comment("订单编号");
            $table->decimal("total_price",10,2)->nullable(false)->default(0)->comment("订单总金额（包含运费）");
            $table->decimal("pay_price",10,2)->nullable(false)->default(0)->comment("实际支付金额（包含运费）");
            $table->decimal("express_price",10,2)->nullable(false)->default(0)->comment("运费");
            $table->string("name")->nullable(true)->default(null)->comment("收货人姓名");
            $table->string("mobile")->nullable(true)->default(null)->comment("收货人联系方式");
            $table->string("address")->nullable(true)->default(null)->comment("收货地址");
            $table->longText("address_data")->nullable(true)->comment("收货地址 json格式"); 
            $table->string("remark",500)->nullable(true)->default(null)->comment("买家备注");
            $table->tinyInteger("order_status")->nullable(false)->default(0)->comment("订单状态 0--待支付 1--已支付待发货 2--已发货待收货 3--已完成 4--已取消 5--待评价 6--已评价 7--待售后 8--已退款");

            $table->integer("pay_type")->nullable(false)->default(0)->comment("支付方式");
            $table->string("pay_name",32)->nullable(true)->default(null)->comment("支付方式名称");
            $table->string("pay_code",32)->nullable(true)->default(null)->comment("支付方式code");
            $table->timestamp("pay_time")->nullable(true)->default(null)->comment("支付时间");
            
            $table->string("transaction_no")->nullable(true)->default(null)->comment("交易单号");
            $table->timestamp("send_time")->nullable(true)->default(null)->comment("发货时间");
            $table->string("express",32)->nullable(true)->default(null)->comment("物流公司名称");
            $table->string("express_no",32)->nullable(true)->default(null)->comment("物流单号");
            $table->string("express_code",32)->nullable(true)->default(null)->comment("物流CODE");
            $table->string("seller_comments",500)->nullable(true)->default(null)->comment("商家备注留言");
            $table->timestamp("confirm_time")->nullable(true)->default(null)->comment("确认收货时间");
            $table->tinyInteger("apply_delete")->nullable(false)->default(0)->comment("是否申请取消订单 已支付未发货时用到 0--否 1--申请取消订单");

            $table->tinyInteger("is_price")->nullable(false)->default(0)->comment("是否发放佣金 0--否 1--是");
            $table->integer("parent_id_1")->nullable(false)->default(0)->comment("父级ID");
            $table->integer("parent_id_2")->nullable(false)->default(0)->comment("用户上二级ID");
            $table->decimal("first_price",10,2)->nullable(false)->default(0)->comment("一级佣金");
            $table->decimal("second_price",10,2)->nullable(false)->default(0)->comment("二级佣金");
            $table->decimal("rebate",10,2)->nullable(false)->default(0)->comment("自购返利");
            $table->decimal("share_price",10,2)->nullable(false)->default(0)->comment("发放佣金金额");

            $table->integer("user_coupon_id")->nullable(false)->default(0)->comment("使用的优惠券ID");
            $table->decimal("coupon_sub_price",10,2)->nullable(false)->default(0)->comment("优惠券抵消金额");

            $table->tinyInteger("is_offline")->nullable(false)->default(0)->comment("是否到底自提 0--否 1--是");
            $table->integer("shop_id")->nullable(false)->default(0)->comment("门店ID");
            $table->decimal("discount",10,2)->nullable(false)->default(0)->comment("会员折扣");
            $table->tinyInteger("type")->nullable(false)->default(0)->comment("订单类型 0--普通订单");
            $table->decimal("integral",10,2)->nullable(false)->default(0)->comment("订单赠送积分");
            $table->tinyInteger("give_integral")->nullable(false)->default(0)->comment("是否发放积分 0--否 1--是");
            $table->tinyInteger("is_recycle")->nullable(false)->default(0)->comment("是否移入回收站 0--否 1--是");
            $table->tinyInteger("is_delete")->nullable(false)->default(0)->comment("是否删除 0--否 1--是");

            $table->timestamps();
            
        });
        DB::statement("ALTER TABLE zzkj_{$this->tableName} comment '订单表'");
    }
}
